package com.beiding.dao.impl;

import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

@Component
public class GenerationManager {

    private final String prefix;
    private ValueOperations<String, Long> opsForValue;

    public GenerationManager(ApplicationContext context, Environment environment) {
        prefix = "__generation_"+environment.getProperty("id");
        
        opsForValue = ((RedisTemplate<String, Long>) context.getBean("redisTemplate")).opsForValue();

        /*--------- 初始化代 ---------*/
        if (opsForValue.get(prefix)==null){
            opsForValue.set(prefix, 0L);
        }
    }
    long generation()
    {
        long c=opsForValue.get(prefix);
        long r = c+1;
        opsForValue.set(prefix, r);
        return r;
    }

    public String getPrefix() {
        return prefix;
    }
}
